/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;

/**
 * The Class HttpSessionListener.
 *
 * <p>
 * This http session listener monitor all sessions created and destroyed of a
 * web application.<br/>
 * <br/>
 * Insert into web.xml the code:<br/>
 * <br/>
 * <code>
 * &lt;web-app&gt;<br/>
 * &nbsp;&lt;listener&gt;<br/>
 * &nbsp;&nbsp;&lt;listener-class&gt;jeeobserver.HttpSessionListener&lt;/listener-class&gt;<br/>
 * &nbsp;&lt;/listener&gt;<br/>
 * &lt;/web-app&gt;<br/>
 * </code>
 * </p>
 *
 * @see HttpSessionEvent
 */
public class HttpSessionListener implements javax.servlet.http.HttpSessionListener {

	/** The http session count. */
	private int httpSessionCount = 0;

	/** The http session observer map. */
	private final Map<String, HttpSessionObserver> httpSessionObserverMap = new HashMap<String, HttpSessionObserver>();

	/**
	 *
	 * @param httpSessionEvent
	 */
	@Override
	public synchronized void sessionCreated(HttpSessionEvent httpSessionEvent) {

		final ServletContext servletContext = httpSessionEvent.getSession().getServletContext();

		String projectName = servletContext.getServletContextName();

		if ((projectName == null) || projectName.isEmpty()) {
			projectName = httpSessionEvent.getSession().getServletContext().getContextPath();
			projectName = projectName.replace("/", "");
		}

		final String sessionId = httpSessionEvent.getSession().getId();

		this.httpSessionCount = this.httpSessionCount + 1;

		final HttpSessionObserver observer = new HttpSessionObserver(projectName, sessionId);
		observer.start();
		this.httpSessionObserverMap.put(sessionId, observer);
	}

	/**
	 *
	 * @param httpSessionEvent
	 */
	@Override
	public synchronized void sessionDestroyed(HttpSessionEvent httpSessionEvent) {

		final String sessionId = httpSessionEvent.getSession().getId();

		this.httpSessionCount = this.httpSessionCount - 1;

		final HttpSessionObserver observer = this.httpSessionObserverMap.get(sessionId);
		if (observer != null) {
			observer.stop();
			this.httpSessionObserverMap.remove(sessionId);
		}
	}
}
